/**
 * weapp-js
 * https://code.aliyun.com/jionglee/weapp_base.git
 * Copyright@ 2021 Motigo, Inc.
 * 
 */
import baseRequest from './base_request.js';
import Log from 'log';
import Callback from './utils/callback.js';
import { promisifyAll } from './polyfill/promisify.js';

// const KEYS = [request, session, ad, ui, payment, audio, preload, file, recorder, softlogin, log];

class Mox extends Callback {
    constructor() {
        super();

        this._ready = false;
        this.appid = '';

        const accountInfo = wx.getAccountInfoSync();
        this.appid = accountInfo.miniProgram.appId;
        baseRequest.appid = this.appid;

        this.log = Log;
    }
    /**
     * mox init
     *
     * @param {boolean} polyfill 是否支持 promise 写法
     *
     *
     */

    init({ polyfill = false, uid = '' } = opts) {
        if (polyfill) {
            promisifyAll(this);
        } else {
            // ignore
        }
        this.stat({
            key: '__visit__',
            uid
        });
    }
    stat(opts) {
        setTimeout(() => {
            baseRequest
                .post(`/data_analytics/pvuv/weapp_${this.appid}`, {
                    key: opts.key,
                    // uid: this.user.unionid,
                    uid: opts.uid,
                    elapsed_time: opts.elapsed_time
                })
                .then(res => {
                    this.success(opts, res);
                })
                .catch(err => {
                    this.fail(opts, err);
                });
        }, 10);
    }
}

let mox = new Mox();

export default mox;
